<template>
  <div>
    <el-button round type="primary" icon="el-icon-circle-plus-outline" @click="addInfo()">增加成绩</el-button>
    <el-dialog :show-close="false" destroy-on-close :modal-append-to-body="false" title="提示" :visible="dialogVisible" width="30%">
      <el-form label-position="right" ref="formName" label-width="80px" :model="formLabelAlign">
        <el-form-item label="学号">
          <el-input v-model="formLabelAlign.stuNo"></el-input>
        </el-form-item>
        <el-form-item label="学校">
          <el-select v-model="formLabelAlign.schoolId" placeholder="请选择">
            <el-option v-for=" item,index in $store.state.school.profile" :key="item.schoolId" :label="item.schoolName" :value="item.schoolId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="课程名">
          <el-select v-model="formLabelAlign.courseId" remote :remote-method="courseSearch" filterable placeholder="可搜索">
            <el-option v-for="item in course" :key="item.courseId" :label="item.coursename" :value="item.courseId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="成绩">
          <el-input v-model="formLabelAlign.score"></el-input>
        </el-form-item>
        <el-form-item label="考试时间">
          <el-date-picker v-model="formLabelAlign.examtime" value-format="yyyy-MM-dd HH:mm:SS" type="datetime" placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeFn('formName')">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { searchCourse } from '@/api/course'
import { addScore } from '@/api/score'
import debounce from '@/utils/debounce'
export default {
  data() {
    return {
      dialogVisible: false,
      formLabelAlign: {
        schoolId: '',
        stuNo: '',
        courseId: '',
        score: '',
        examtime: ''
      },
      course: []
    }
  },
  methods: {
    courseSearch(coursename) {
      if (coursename === '') {
        return
      }
      debounce(
        searchCourse(coursename).then(res => {
          this.course = res.data
        }),
        1000
      )
    },
    submit() {
      addScore(this.formLabelAlign).then(res => {
        if (res.data) {
          this.$message({
            type: 'success',
            message: res.msg
          })
          this.$emit('addChange')
          this.closeFn()
        }
      })
    },
    addInfo() {
      this.dialogVisible = true
    },
    closeFn(formName) {
      this.formLabelAlign = {
        schoolId: '',
        stuNo: '',
        coursename: '',
        score: '',
        examtime: ''
      }
      this.dialogVisible = false
    }
  }
}
</script>

<style lang="less" scoped>
.editBox {
  padding-right: 10px;
}
</style>